from pysrcds.servers.srcds import _SourceServer,ServerError
from utilities import Environment,logging
from third_party import path
import os

# global data path - might be needed
data_path = Environment.userdata / "pysrcds/servers/"

# Server implements _SourceServer
class Server(_SourceServer):
    '''
        Our specific source server
    '''
    def __init__(self,name,alt_directory=None):
        '''
            Setup the server
                # name:             name of the server
                # alt_directory:    use a different cfg directory rather than a generated one
        '''
       
        # setup the right path for the server
        if not alt_directory:
            self.dir = data_path / str(name)
        else:
            self.dir = path(alt_directory)
        # init the base class (saves on some code
        
        _SourceServer.__init__(self,name)
        
    def _load_defaults(self):
        # load the cstrike cfg into the main server
        self._parse_cfg((data_path / "defaults/cstrike.cfg"))
        
    def _load_from_directory(self):
        # first check the file structure
        logging.log(0, "[PySrcds:Info] Verifying server directory structure prior to startup")
        
        # do we have a cfg directory
        cfgdir = self.dir / "cfg"
        if not cfgdir.exists():
            # no cfg file
            raise ServerError("Directory structure invalid when loading server: cfg folder not found")
            
        # ok cfg is there, lets test the files
        for file in ["server.cfg"]:
            if file not in cfgdir.files():
                raise ServerError("File missing when verifying directory structure: %s")
            # ok the file is there - so parse it out
            self._parse_cfg((cfgdir / file))
        
    def _copy_default(self):
        pass
        
